<template>
  <el-main>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '' }">角色管理</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/auth/rule' }"
        >角色列表</el-breadcrumb-item
      >
      <el-breadcrumb-item>修改角色信息</el-breadcrumb-item>
    </el-breadcrumb>
    <div class="margin-top-20">
      <el-card shadow="never">
        <el-form
          :model="form"
          :rules="rules"
          label-width="80px"
          ref="form"
          style=" width: 400px;"
        >
          <el-form-item label="角色名称" prop="groupName">
            <el-input v-model="form.groupName"></el-input>
          </el-form-item>
          <el-form-item label="是否激活">
            <el-switch
              active-text="是"
              inactive-text="否"
              v-model="form.upLock"
            ></el-switch>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input type="textarea" v-model="form.remark"></el-input>
          </el-form-item>

          <el-form-item>
            <el-button @click="submitForm('form')" type="primary"
              >立即提交
            </el-button>
            <el-button @click="cancelForm">取消</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </el-main>
</template>

<script>
export default {
  data() {
    return {
      id: null,
      form: {
        groupName: "",
        upLock: false,
        remark: null
      },
      rules: {
        groupName: [
          { required: true, message: "请输入角色名", trigger: "none" },
          { min: 2, max: 25, message: "长度在 2 到 25 个字符", trigger: "none" }
        ]
      }
    };
  },
  mounted() {
    this.id = this.$route.params.id;
    this.info();
  },
  methods: {
    info() {
      this.$ajax.auth_group
        .info(this.id)
        .then(result => {
          if (result.code != 1) {
            this.$message({
              message: result.msg,
              type: "warning"
            });
            this.$router.push("/auth/group");
          } else {
            this.form = result.data;
          }
        })
        .catch(e => {
          console.log(e);
          this.$message({
            message: "系统繁忙，请稍后重试",
            type: "warning"
          });
        });
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let loadingInstance = this.$loading({ fullscreen: true });
          this.$ajax.auth_group
            .update(this.form)
            .then(result => {
              if (result.code != 1) {
                this.$message({
                  message: result.msg,
                  type: "warning"
                });
              } else {
                this.$message({
                  type: "success",
                  message: "提交成功!"
                });
                this.$router.push("/auth/group");
              }
            })
            .catch(e => {
              console.log(e);
              this.$message({
                message: "系统繁忙，请稍后重试",
                type: "warning"
              });
            })
            .finally(function() {
              loadingInstance.close();
            });
        } else {
          return false;
        }
      });
    },
    cancelForm() {
      this.$router.push("/auth/group");
    }
  }
};
</script>
